<?php
	include_once("../backend/dataprovider.php");
	session_start();
	
	if(isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800)) {
		// last request was more than 30 minutes ago
		session_unset();     // unset $_SESSION variable for the run-time 
		session_destroy();   // destroy session data in storage
	}
	$_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp
	
	if( !isset($_SESSION["loggedUser"]) || $_SESSION["loggedUser"]==null){
		header("Location:../index.php");
		return;
	}
	if(isset($_GET["txtUsername"]))
		$sql = "select username,fname, lname, u.createdDate, u.active, count(ul.id) as numOfLogin, 
			(select max(login_time) from user_log where userId=u.id) as LastLogin
			from users as u left join user_log as ul on u.id = ul.userId
			where u.username like '%" . $_GET["txtUsername"] . "%' group by username, fname, lname, u.createdDate, u.active";
	else
		$sql = "select username,fname, lname, u.createdDate, u.active, count(ul.id) as numOfLogin, 
			(select max(login_time) from user_log where userId=u.id) as LastLogin
			from users as u left join user_log as ul on u.id = ul.userId
			group by username, fname, lname, u.createdDate, u.active";
		
?>
<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<link rel="stylesheet" type="text/css" href="admin.css"/>
		<script type='text/javascript' src='../jquery-min.js'></script>
		<script type="text/javascript">
			$(document).ready(function(){
				$("#btnSearch").click(function(){
					$("#frmSearchUser").submit();
				});
				
				$("#btnReg").click(function(){
					window.location.replace("reg.php");
					/*
						OR we can use
						window.location.href = "";
					*/
				});
				
				$("#btnLogout").click(function(){
					$.ajax({
						url:"backend/user.php",
						cache:false,
						type:'POST',
						data:{ACTION:'LOGOUT'},
						success:function(responseText){
							//alert(responseText);
							
							var rs = $.parseJSON(responseText);
							$('#div_user_pane').fadeOut();
							$('#div_login').fadeIn();
							
						}
					});
					return false;
				});
			});
			
		</script>
		
		<title>VTech blog - A technical blog</title>
	</head>
	<body>
		<div id="container">
			<div id="div_header">
				<div id="div_user_pane">
					<span style="padding-left:30px;color:#dafafa;">Xin chao </span>
					<a href="#" style="text-decoration:none;color:#ffff22;" ><span style="font-weight:bold;"><?php echo $_SESSION["loggedUser"]; ?></span></a>
					<a href="#" style="text-decoration:none;color:#ffdada" id="btnLogout">Logout</a>
				</div>
			</div>
			<div id="div_nav" class="menu">
				<ul>
					<li><a href="../index.php">Home</a></li>
					<li><a href="../funct.php">NMath</a></li>
					<?php
						if(isset($_SESSION["loggedUser"])){
							if($_SESSION["loggedUser"]=='davu' || $_SESSION["loggedUser"]=='admin'|| $_SESSION["loggedUser"]=='huong'){
								print "<li><a href='dashboard.php'>Administrative</a></li>";
							}
						}
					?>
					<li><a href="../about.php">About me</a></li>
				</ul>
			</div>
			<div id="sidebar">
				<ul style="list-style-type:none">
					<li><a href="dashboard.php">Dashboard</a></li>
					<li>Users</li>
					<li><a href="articles.php">Posts</a></li>
					<li><a href="files.php">Files</a></li>
					<li><a href="../about.php">Application</a></li>
				</ul>
			</div>
			<div id="div_main">
				<div>
					<form id="frmSearchUser" name="frmSearchUser" action="userlst.php">
						<input type="text" name="txtUsername" class="searchText" />
						<input type="submit" name="btnSearch" value="Search"/>
					</form>
				</div>
				<table class="tbl-data">
					<thead>
						<tr>
							<th>No</th>
							<th>Username</th>
							<th class="tdlong">Fullname</th>
							<th class="tdlong">Joined Date</th>
							<th>#Login</th>
							<th class="tdlong">Last Login</th>
							<th class="tdlong">Active</th>
						</tr>
					</thead>
					<tbody>
				<?php
					$postLst = DataProvider::executeQuery($sql);
					if($postLst != false){
						$idx = 1;
						while($row = $postLst->fetch_assoc()){ ?>
							<tr>
								<td><?php print $idx; ?></td>
								<td><?php print $row["username"]; ?></td>
								<td class="tdlong"><?php print ($row["fname"]." ".$row["lname"]); ?></td>
								<td class="tdlong"><?php print $row["createdDate"]; ?></td>
								<td><?php print $row["numOfLogin"]; ?></td>
								<td class="tdlong"><?php print $row["LastLogin"]; ?></td>
								<td><input type="checkbox" name="actives" value="yes" <?php print ($row["active"]==1)?"checked":""; ?>></td>
							</tr>
						<?php $idx++; }
					}
				?>
				</tbody>
				</table>
			</div>
			<div id="div_footer" >Copyright 2013 @Dao Anh Vu</div>
		</div>
	</body>
</html>
